<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * This file contains the Form_Controller class.
 *
 * PHP version 5
 *
 * This file is part of Majic - A rapid-development web application framework
 * for PHP 5.
 *
 * Majic is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * Majic is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Majic. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     Form
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 * @copyright   2007-2008 Michael J. I. Jackson
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
 * @version     SVN: $Id: Controller.php 34 2008-07-19 04:50:53Z mjijackson $
 */

/**
 * Provides methods convenient for dealing with multi-page forms.
 *
 * @package     Form
 * @author      Michael J. I. Jackson <mjijackson@gmail.com>
 */
class Form_Controller
{

    /**
     * Contains this form controller's pages.
     *
     * @var     array
     * @access  private
     */
    private $pages;

    /**
     * Constructor.
     *
     * @access  public
     */
    public function __construct() {}

    /**
     * Adds a page to this form controller.
     *
     * @param   Form_Page       $page       The page to add
     * @return  void
     * @access  public
     */
    public function addPage(Form_Page $page)
    {
        $page->setController($this);
        $this->pages[$page->name] = false;
    }

    /**
     * Validates all pages in this form controller. Returns true if all pages
     * are valid. Otherwise, returns the name of the first invalid page.
     *
     * @return  mixed       True if the controller validates, name of the first
     *                      invalid page otherwise
     * @access  public
     */
    public function validate()
    {
        foreach ($this->pages as $page => $valid) {
            if ($valid === false) {
                return $page;
            }
        }
        return true;
    }

    /**
     * Gets the previous page for this application according to its path.
     *
     * @param   string  $current    The name of the current page
     * @return  string              The name of the previous page
     * @access  public
     */
    public function prev($current)
    {
        $prev = null;
        foreach ($this->pages as $page => $valid) {
            if ($page == $current) {
                return $prev;
            }
            $prev = $page;
        }
        return null;
    }

    /**
     * Gets the name of the next page in this controller based on the given
     * current page. If the current page is the last page, returns null.
     *
     * @param   string  $current    The name of the current page
     * @return  string              The name of the next page
     * @access  public
     */
    public function next($current)
    {
        $pages = array_keys($this->pages);
        foreach ($pages as $index => $page) {
            if ($current == $page) {
                return isset($pages[$index+1]) ? $pages[$index+1] : null;
            }
        }
    }

}

?>
